package com.bihu.bihudemo.auth.compoent;

import com.alibaba.fastjson.JSON;
import com.bihu.bihudemo.myenum.ResultCode;
import com.bihu.bihudemo.result.JsonResult;
import com.bihu.bihudemo.utils.ResultTool;
import org.springframework.security.web.session.SessionInformationExpiredEvent;
import org.springframework.security.web.session.SessionInformationExpiredStrategy;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//自定义session 会话异常（各种异常，比如被挤下线）返回值
@Component
public class CustomizeSessionInformationExpiredStrategy  implements SessionInformationExpiredStrategy {
    @Override
    public void onExpiredSessionDetected(SessionInformationExpiredEvent sessionInformationExpiredEvent) throws IOException, ServletException {

        JsonResult result = ResultTool.fail(ResultCode.USER_ACCOUNT_USE_BY_OTHERS);
        HttpServletResponse response = sessionInformationExpiredEvent.getResponse();
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(JSON.toJSONString(result));
    }
}
